<?php
/**
 * Created by PhpStorm.
 * User: liujialiang
 * Date: 2017/9/20
 * Time: 11:00
 */

namespace App\Controller;

use app\Controller;
use App\Model\UserModel;
use lib\View;

class IndexController extends Controller
{
    public function index(){
        $route=$this->getRoute();
        $_SESSION['route']=$route;
        $userinfo=$_SESSION['sns_user'];
        return (new View('index',['userinfo'=>$userinfo]));
    }

    public function lock(){
        return new View('user.lock');
    }

    public function unlock(){
        if(IS_POST){
            if($_POST['password']==$_SESSION['sns_user']['password']){
                success('解锁成功!',$_POST['index']);
            }else{
                error('密码错误!');
            }
        }
    }

    public function profile(){
        $userinfo=$_SESSION['sns_user'];
        return new View('profile.index',['userinfo'=>$userinfo]);
    }

    public function profileEdit(){
        $userinfo=$_SESSION['sns_user'];
        if(IS_POST){
            $data=$_POST;
            $data['id']=$_SESSION['sns_user']['id'];

            $birthday=explode('-',$data['birthday']);
            if(($birthday['1']==1 && $birthday['2']>=20) ||($birthday['2']==2 && $birthday['2']<=18) ){
                $data['constellation']='水瓶座';

            }else if(($birthday['1']==2 && $birthday['2']>18) ||($birthday['2']==3 && $birthday['2']<=20)){
                $data['constellation']='双鱼座';

            }else if(($birthday['1']==3 && $birthday['2']>20) ||($birthday['2']==4 && $birthday['2']<=19)){
                $data['constellation']='白羊座';

            }else if(($birthday['1']==4 && $birthday['2']>19) ||($birthday['2']==5 && $birthday['2']<=20)){
                $data['constellation']='金牛座';

            }else if(($birthday['1']==5 && $birthday['2']>20) ||($birthday['2']==6 && $birthday['2']<=21)){
                $data['constellation']='双子座';

            }else if(($birthday['1']==6 && $birthday['2']>21) ||($birthday['2']==7 && $birthday['2']<=22)){
                $data['constellation']='巨蟹座';

            }else if(($birthday['1']==7 && $birthday['2']>22) ||($birthday['2']==8 && $birthday['2']<=22)){
                $data['constellation']='狮子座';

            }else if(($birthday['1']==8 && $birthday['2']>22) ||($birthday['2']==9 && $birthday['2']<=22)){
                $data['constellation']='处女座';

            }else if(($birthday['1']==9 && $birthday['2']>22) ||($birthday['2']==10 && $birthday['2']<=23)){
                $data['constellation']='天秤座';
            }else if(($birthday['1']==10 && $birthday['2']>23) ||($birthday['2']==11 && $birthday['2']<=22)){
                $data['constellation']='天蝎座';

            }else if(($birthday['1']==11 && $birthday['2']>22) ||($birthday['2']==12 && $birthday['2']<=21)){
                $data['constellation']='射手座';

            }else if(($birthday['1']==12 && $birthday['2']>21) ||($birthday['2']==1 && $birthday['2']<=19)){
                $data['constellation']='摩羯座';
            }


            $user=new UserModel();
            $res=$user->save($data);
            $userData=$user->get(['id'=>$data['id']]);
            $_SESSION['sns_user']=$userData[0];
            if($res){
                success('修改成功!','profile');
            }else{
                error('修改失败!');
            }
        }
        return new View('profile.edit',['userinfo'=>$userinfo]);
    }
}